package com.aizhixin.lab.project.course.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpResponse;
import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.project.course.domain.*;
import com.aizhixin.lab.project.course.entity.ProjectClassStu;
import com.aizhixin.lab.project.course.entity.ProjectGroup;
import com.aizhixin.lab.project.course.entity.ProjectGroupType;
import com.aizhixin.lab.project.course.repository.ProjectGroupRepository;
import com.aizhixin.lab.project.course.repository.ProjectGroupTypeRepository;
import com.aizhixin.lab.project.course.service.ProjectClassService;
import com.aizhixin.lab.project.course.service.ProjectClassStuService;
import com.aizhixin.lab.project.course.service.ProjectGroupService;
import com.aizhixin.lab.project.course.vo.*;
import io.swagger.annotations.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.Timestamp;
import java.util.*;

@RequestMapping("/api/v1/teacher/project/group")
@RestController
@Api(value = "小组管理模块", tags = "小组管理")
public class ProjectGroupController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private ProjectGroupService projectGroupService;
    @Autowired
    private ProjectGroupRepository projectGroupRepository;
    @Autowired
    private ProjectClassStuService projectClassStuService;
    @Autowired
    private ProjectGroupTypeRepository projectGroupTypeRepository;

    @RequestMapping(value = "/exportProjectStuList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "导出该项目下学生列表", httpMethod = "GET", notes = "导出该项目下学生列表")
    public ResponseEntity<?> exportProjectStuList(@RequestHeader("Authorization") String token, @RequestParam(value = "projectId") String projectId, HttpServletResponse response) throws IOException {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        int count = 0;
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        HSSFWorkbook wb= projectClassStuService.getProjectStuList(projectId);
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(System.currentTimeMillis() + ".xlsx", "UTF-8"));
//        response.addHeader("Content-Length", "" + file.length());
        response.setContentType("application/octet-stream");
        ServletOutputStream outputStream = response.getOutputStream();
        wb.write(outputStream);

        outputStream.close();
        return new ResponseEntity(count, HttpStatus.OK);
    }
    @RequestMapping(value = "/importTemplate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "导入分组", httpMethod = "POST", notes = "导入分组<br><br><b>@author lwq</b>")
    public ResponseEntity<?> importTemplate(@RequestHeader("Authorization") String token,
                                            @ApiParam(value = "项目Id", required = true) String projectId,
                                            @ApiParam(value = "Excel数据文件", required = true) @RequestParam(value = "file") MultipartFile file) throws IOException {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        int count = 0;
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        result = projectClassStuService.importProjectStuList(file, projectId);
        return new ResponseEntity(result, HttpStatus.OK);
    }
        @RequestMapping(value = "/findProjectNum", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询项目组人数", httpMethod = "GET", notes = "查询项目组人数")
    public ResponseEntity<?> findProjectNum(@RequestHeader("Authorization") String token, @RequestParam(value = "projectId") String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        int count = 0;
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            count = projectGroupService.findProjectNum(projectId);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }

        return new ResponseEntity(count, HttpStatus.OK);
    }

    @RequestMapping(value = "/projectIdFindGroupId", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "通过项目ID查询小组ID", httpMethod = "GET", notes = "通过项目ID查询小组ID")
    public ResponseEntity<?> projectIdFindGroupId(@RequestHeader("Authorization") String token, @RequestParam(value = "projectId") String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        String count = "";
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            count = projectGroupService.projectIdFindGroupId(projectId, account.getId());
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }

        return new ResponseEntity(count, HttpStatus.OK);
    }
    @RequestMapping(value = "/groupFindProjectId", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "通过小组ID查询练习项目ID和备选项目ID", httpMethod = "GET", notes = "通过小组ID查询练习项目ID和备选项目ID")
    public ResponseEntity<?> groupFindProjectId(@RequestHeader("Authorization") String token, @RequestParam(value = "groupId") String groupId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
           result= projectGroupService.findGroupOtherProjectId(groupId);


        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findProjectGroupList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "通过项目ID查询小组及成员列表", httpMethod = "GET", notes = "通过项目ID查询小组及成员列表")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = GroupInfoVo.class),})
    public ResponseEntity<?> findProjectGroupList(@RequestHeader("Authorization") String token, @RequestParam(value = "projectId") String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }

            List<GroupInfoVo> projectGroupInfo = projectGroupService.findProjectGroupInfo(projectId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, projectGroupInfo);


        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/stuRandomGroup", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "班级学生随机分组", httpMethod = "POST", notes = "班级学生随机分组")
    public ResponseEntity<?> stuRandomGroup(@RequestHeader("Authorization") String token, @RequestBody StuRandomDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            projectGroupService.stuRandomGroup(domain);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }



    @RequestMapping(value = "/stuCreationGroup", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "自由分组-创建小组", httpMethod = "POST", notes = "自由分组-创建小组")
    public ResponseEntity<?> stuCreationGroup(@RequestHeader("Authorization") String token, @RequestBody StuCreationGroupDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            ProjectGroupType projectGroupType = projectGroupTypeRepository.findByProjectIdAndDeleteFlag(domain.getProjectId(), 0);
            if (projectGroupType != null && DateUtil.date().before(projectGroupType.getEndTime())) {
                ProjectGroup projectGroup = projectGroupService.stuCreationGroup(domain, account.getId());
                result.put(ApiReturnConstants.SUCCESS, true);
                result.put(ApiReturnConstants.DATA, projectGroup);
            } else {
                result.put(ApiReturnConstants.SUCCESS, false);
                result.put(ApiReturnConstants.MESSAGE, "已超过创建时间或没有选择自由分组！");
            }

        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }


    @RequestMapping(value = "/applyGroupLeader", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "申请成为组长", httpMethod = "POST", notes = "申请成为组长")
    public ResponseEntity<?> applyGroupLeader(@RequestHeader("Authorization") String token, @RequestBody ApplyGroupLeaderDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            projectGroupService.applyGroupLeader(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.MESSAGE, "申请成功");

        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }


    @RequestMapping(value = "/stuAddGroup", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "自由分组-加入小组", httpMethod = "POST", notes = "自由分组-加入小组")
    public ResponseEntity<?> stuAddGroup(@RequestHeader("Authorization") String token, @RequestBody StuAddGroupDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }

        result = projectGroupService.stuAddGroup(domain);

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/stuDeleteGroup", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "自由分组-踢出成员", httpMethod = "POST", notes = "自由分组-踢出成员")
    public ResponseEntity<?> stuDeleteGroup(@RequestHeader("Authorization") String token, @RequestBody StuAddGroupDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            String projectGroup = projectGroupService.stuDeleteGroup(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, projectGroup);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    /*@RequestMapping(value = "/findGroupMember", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询小组成员", httpMethod = "POST", notes = "查询小组成员")
    public ResponseEntity<?> findGroupMember(@RequestHeader("Authorization") String token, @RequestBody findGroupMemberDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }try{
            List<ProjectClassStu> stuList = projectGroupService.findGroupMember(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,stuList);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }*/

    @RequestMapping(value = "/findProjectGroup", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询项目小组信息", httpMethod = "POST", notes = "查询项目小组信息")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProjectGroup.class),})
    public ResponseEntity<?> findProjectGroup(@RequestHeader("Authorization") String token, @RequestBody QueryProjectGroupDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            List<ProjectGroup> projectGroupList = projectGroupRepository.findByProjectId(domain.getProjectId());
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, projectGroupList);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findGroupMember", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询小组成员列表", httpMethod = "POST", notes = "查询小组成员列表")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProjectClassStutVo.class),})
    public ResponseEntity<?> findGroupMember(@RequestHeader("Authorization") String token, @RequestBody QueryProjectGroupMemberDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            List<ProjectClassStutVo> projectGroupList = projectClassStuService.findProjectGroupMember(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, projectGroupList);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveGroupSetting", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存分组设置", httpMethod = "POST", notes = "保存分组设置")
    public ResponseEntity<?> saveGroupSetting(@RequestHeader("Authorization") String token, @RequestBody ProjectGroupTypeDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<GroupInfoVo> projectGroup = projectGroupService.findProjectGroupInfo(domain.getProjectId());
        if (projectGroup.size()>0) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "已有分组设置");
            return new ResponseEntity(result, HttpStatus.OK);
        }
        projectGroupService.saveGroupSetting(domain);
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.MESSAGE, "ok");

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/updateRole", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改小组学生开发角色", httpMethod = "POST", notes = "修改小组学生开发角色")
    public ResponseEntity<?> updateRole(@RequestHeader("Authorization") String token, @RequestBody ProjectGroupStuRoleTypeDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            projectGroupService.updateRole(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.MESSAGE, "ok");
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/releaseNotice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "发布公告", httpMethod = "POST", notes = "发布公告")
    public ResponseEntity<?> releaseNotice(@RequestHeader("Authorization") String token, @RequestBody NoticeDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            result = projectGroupService.releaseNotice(domain);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findProjectGroupList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询项目下团队列表", httpMethod = "POST", notes = "查询项目下团队列表")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProjectGroupVo.class),})
    public ResponseEntity<?> findProjectGroupList(@RequestHeader("Authorization") String token, @RequestBody QueryGroupDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }

            result = projectClassStuService.findProjectGroup(domain);

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findProjectGroupMember", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询团队下成员列表", httpMethod = "GET", notes = "查询团队下成员列表")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProjectGroupMemberVo.class),})
    public ResponseEntity<?> findProjectGroupMember(@RequestHeader("Authorization") String token, @RequestParam String id,
                                                    @RequestParam String projectId ) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            List<ProjectGroupMemberVo> projectGroupMember = projectClassStuService.findProjectGroupMember(id,projectId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, projectGroupMember);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findGroupType", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看分组类型", httpMethod = "GET", notes = "查看小组分组类型")
    public ResponseEntity<?> findGroupType(@RequestHeader("Authorization") String token, @RequestParam String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            result = projectGroupService.findGroupType(projectId, account.getId());
            result.put(ApiReturnConstants.SUCCESS, true);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }


    @RequestMapping(value = "/findProjectGroupTaskCommit", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看小组成员任务完成情况", httpMethod = "GET", notes = "查看小组成员任务完成情况")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProjectGroupTaskCommitVo.class),})
    public ResponseEntity<?> findProjectGroupTaskCommit(@RequestHeader("Authorization") String token, @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            List<ProjectGroupTaskCommitVo> projectGroupTaskCommit = projectClassStuService.findProjectGroupTaskCommit(id);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, projectGroupTaskCommit);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findProjectGroupInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看小组信息", httpMethod = "GET", notes = "查看小组信息")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProjectGroup.class),})
    public ResponseEntity<?> findProjectGroupInfo(@RequestHeader("Authorization") String token, @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            ProjectGroup projectGroup = projectGroupRepository.findOne(id);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, projectGroup);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/like", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "小组点赞", httpMethod = "GET", notes = "点赞<br><br><b>lwq</b>")
    public ResponseEntity<?> like(@RequestHeader("Authorization") String token, @RequestParam String groupId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            result = projectGroupService.likeOrUrge(groupId, 10);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/urge", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "小组督促", httpMethod = "GET", notes = "督促<br><br><b>lwq</b>")
    public ResponseEntity<?> urge(@RequestHeader("Authorization") String token, @RequestParam String groupId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try {
            result = projectGroupService.likeOrUrge(groupId, 20);
        } catch (Exception e) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/deleteGroup", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除小组", httpMethod = "GET", notes = "删除小组<br><br><b>lwq</b>")
    public ResponseEntity<?> deleteGroup(@RequestHeader("Authorization") String token, @RequestParam String groupId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }

            result = projectGroupService.deleteGroup(groupId);


        return new ResponseEntity(result, HttpStatus.OK);
    }
}
